<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:cc="http://xmlns.jcp.org/jsf/composite/ezcomp"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">

    <body>
        <ui:composition template="./../WEB-INF/template/user/userPageTemplate.xhtml">

            <ui:define name="title">
                User Home
            </ui:define>

            <ui:define name="events">
                <h:form id="eventForm">
                    <p:dataList value="#{userHome.eventsList}" var="event"
                                paginator="true" rows="10" paginatorTemplate=" {PreviousPageLink} {CurrentPageReport} {NextPageLink}"
                                paginatorPosition="top" id="event-paginator"
                                emptyMessage="No events in calendar"
                                >


                        <p:commandLink update="_calendarForm_eventSummary" actionListener="#{userHome.setSelectedEvent(event)}" oncomplete="PF('eventSummary').show();" >
                            
                                #{event.title} 
                            

                            <h:outputText value="#{event.startingDate}" >
                                <f:convertDateTime pattern="dd/MM/yyyy" />
                            </h:outputText>
                       

                        </p:commandLink>
                    </p:dataList>
                </h:form>
            </ui:define>

            <ui:define name="notifications">
                <h:form id="notificationForm" > 
                    <p:dataList value="#{notificationBean.notifications}" var="notification" 
                                emptyMessage="No notification present"
                                paginator="true" rows="10"  paginatorTemplate=" {PreviousPageLink} {CurrentPageReport} {NextPageLink}"
                                paginatorPosition="top" id="notification-paginator">

                        <p:commandLink  update="_notificationDetailForm_details" oncomplete="PF('notificationDialog').show();"

                                        title="View Detail" value="#{notificationBean.getNotificationText(notification)}">
                            <f:setPropertyActionListener value="#{notification}" target="#{notificationBean.notificationSelected}" />
                            <f:setPropertyActionListener value="#{notification.eventId}" target="#{userHome.eventInvited}" />
                        </p:commandLink>
                    </p:dataList>


                    <p:poll interval="#{notificationBean.UPDATE_INTERVALL}" update="_notificationForm_notification-paginator" listener="#{notificationBean.updateNotifications()}" />
                </h:form>


            </ui:define>



            <ui:define name="content">


                <h:form id="notificationDetailForm">
                    <p:dialog header="#{userHome.notificationDetail().title}" widgetVar="notificationDialog"   width="400" resizable="false" id="details">
                        <p:outputPanel >
                            <p>
                                <h:outputLabel value="Description" styleClass="bold-label"/> <br/>
                                <p:scrollPanel mode="native" style="width:350px;height:200px; margin:auto;">
                                    <h:outputText value="#{userHome.notificationDetail().description}"/> <br/>
                                </p:scrollPanel>
                            </p>
                            <c:if test="#{notificationBean.eventType != eventDetailBean.OWN_EVENT}">
                                <p>
                                    <h:outputLabel value="Creator: " styleClass="bold-label"/>   
                                    <h:outputText value="#{userHome.notificationDetail().creator.name} #{userHome.notificationDetail().creator.surname}" />
                                </p>
                            </c:if>


                            <p>
                                <h:outputLabel value="Starting Date: " styleClass="bold-label"/>   
                                <h:outputText value="#{userHome.notificationDetail().startingDate}" >
                                    <f:convertDateTime pattern="dd/MM/yyyy HH:mm" />
                                </h:outputText>
                            </p>
                            <p>
                                <h:outputLabel value="Ending Date: " styleClass="bold-label"/> 
                                <h:outputText value="#{userHome.notificationDetail().endingTime}" > 
                                    <f:convertDateTime pattern="dd/MM/yyyy HH:mm" />
                                </h:outputText>
                            </p>
                            <p>
                                <h:outputLabel value="Location" styleClass="bold-label"/> <br/>
                                <h:outputText value="#{userHome.notificationDetail().location}" rendered="#{not empty userHome.notificationDetail().location}"/>

                            </p>
                            <p>
                                <h:outputLabel value="#{not empty userHome.notificationDetail().outdoor? 'Event is ourdoor' : 'Event is indoor' }"  styleClass="bold-label"/>
                            </p>

                            <p>

                                <c:choose>
                                    <c:when test="#{notificationBean.eventType == eventDetailBean.IN_CALENDAR_EVENT}">

                                        <p:commandButton value="Delete Event" action="#{acceptedEvent.delete(event)}" 
                                                         oncomplete="PF('notificationDialog').hide()">
                                            <f:setPropertyActionListener target="#{acceptedEvent.notification}" value="#{notificationBean.notificationSelected}"/>
                                        </p:commandButton>

                                    </c:when>
                                    <c:when test="#{notificationBean.eventType == eventDetailBean.INVITED_EVENT}">







                                        <p>
                                            <c:if test="#{not empty invitedEvent.showOverlapping(userHome.eventInvited) }" >
                                                <h:outputLabel value="These are the events overlapping with this event" styleClass="bold-label"/> <br/>
                                                <p:dataList value="#{invitedEvent.showOverlapping(userHome.eventInvited)}" var="event" emptyMessage="No overlapping">
                                                    <h:outputText value="#{event.title}"/>
                                                </p:dataList>
                                            </c:if>

                                        </p>







                                        <p:commandButton value="Accept" action="#{invitedEvent.accept(userHome.notificationDetail())}" 
                                                         oncomplete="PF('notificationDialog').hide()" >
                                            <f:setPropertyActionListener target="#{invitedEvent.notification}" value="#{notificationBean.notificationSelected}"/>
                                        </p:commandButton>

                                        <p:commandButton value="Decline" action="#{invitedEvent.decline(userHome.notificationDetail())}" 
                                                         oncomplete="PF('notificationDialog').hide()">
                                            <f:setPropertyActionListener target="#{invitedEvent.notification}" value="#{notificationBean.notificationSelected}"/>
                                        </p:commandButton>
                                    </c:when>
                                    <c:when test="#{notificationBean.eventType == eventDetailBean.OWN_EVENT}">       
                                        <p:commandButton actionListener="#{createEvent.setEventToCreate(userHome.selectedEvent)}" value="Edit" 
                                                         oncomplete="PF('eventSummary').hide();PF('eventDialog').show();" update="_creation_eventDetails"/>
                                        <p:commandButton value="Delete" action="#{ownEvent.delete(userHome.notificationDetail())}" 
                                                         oncomplete="PF('notificationDialog').hide()">
                                            <f:setPropertyActionListener target="#{ownEvent.notification}" value="#{notificationBean.notificationSelected}"/>
                                        </p:commandButton>
                                    </c:when>
                                </c:choose>
                            </p>

                        </p:outputPanel>
                    </p:dialog>
                </h:form>

                <h:form id="calendarForm">
                    <p:schedule id="calendar" styleClass="calendar-style" 
                                value="#{userHome.eventModel}" 
                                widgetVar="myschedule" tooltip="true" draggable="false"
                                ignoreTimezone="false"


                                >
                        <p:ajax event="dateSelect" listener="#{createEvent.onDateSelect}" update="_creation_eventDetails" oncomplete="PF('eventDialog').show();" />
                        <p:ajax event="eventSelect" listener="#{userHome.onEventSelect}" update="eventSummary" oncomplete="PF('eventSummary').show();" />

                    </p:schedule>

                    <p:dialog widgetVar="eventSummary" id="eventSummary" header="#{userHome.selectedEvent.title}" >

                        <p:outputPanel style="height: 450px;">
                            <h:outputLabel value="Title"  styleClass="bold-label"/> <br/>
                            <h:outputText value="#{userHome.selectedEvent.title}"/>

                            <p>
                                <h:outputLabel value="Description" styleClass="bold-label"/> <br/>
                                <p:scrollPanel mode="native" style="width:350px;height:200px; margin:auto;">
                                    <h:outputText value="#{userHome.selectedEvent.description}"/>
                                </p:scrollPanel>
                            </p>
                            <p>
                                <h:outputLabel value="From: " styleClass="bold-label"/>
                                <h:outputText value="#{userHome.selectedEvent.startingDate}">
                                    <f:convertDateTime pattern="dd/MM/yyyy HH:mm" />
                                </h:outputText>
                            </p>

                            <p>
                                <h:outputLabel value="To: " styleClass="bold-label"/>
                                <h:outputText value="#{userHome.selectedEvent.endingTime}">
                                    <f:convertDateTime pattern="dd/MM/yyyy HH:mm" />
                                </h:outputText>
                            </p>
                            <p>
                                <h:outputLabel value="Creator: " styleClass="bold-label"/>   
                                <h:outputText value="#{userHome.selectedEvent.creator.name} #{userHome.selectedEvent.creator.surname}"/>
                            </p>

                            <p>
                                <h:outputLabel value="#{userHome.selectedEvent.visibility ? 'Event is public' : 'Event is private' }"  styleClass="bold-label"/>
                            </p>
                            <p>
                                <h:outputLabel value="#{userHome.selectedEvent.outdoor ? 'Event is ourdoor' : 'Event is indoor' }"  styleClass="bold-label"/>
                            </p>

                            <p>
                                <c:if test="#{not empty userHome.selectedEvent.location}" >
                                    <h:outputLabel value="Location: " styleClass="bold-label" />
                                    <h:outputText value="#{userHome.selectedEvent.location}"/>
                                </c:if>
                            </p>
                            <p>
                                <c:if test="#{not empty userHome.selectedEvent.weatherInformationCollection}">
                                    <h:outputLabel value="Weather: " styleClass="bold-label"/>
                                    <p:dataList value="#{userHome.selectedEvent.weatherInformationCollection}" var="weather" emptyMessage="No weather Information">
                                        <h:outputText value="#{weather.details}"/>
                                    </p:dataList>
                                </c:if>
                            </p>
                            <p>

                            </p>

                            <p>
                                <p:outputLabel value="Accepted Invitation" styleClass="bold-label"/>
                                <p:dataList paginator="true" rows="5" value="#{userHome.selectedEvent.calendarCollection}" var="calendar" type="unordered" paginatorTemplate=" {PreviousPageLink} {CurrentPageReport} {NextPageLink}"
                                            paginatorPosition="top">
                                    <h:outputText value="#{calendar.user.email}" rendered="#{calenadar.user.email ne userHome.selectedEvent.creator.email }"/>
                                </p:dataList>
                            </p>
                            <p>
                                <p:outputLabel value="Not Yet Accepted" styleClass="bold-label"/>
                                <p:dataList paginator="true" rows="5" value="#{notificationBean.getInvitationNotificationForEvent(userHome.selectedEvent)}" var="notification" type="unordered" paginatorTemplate=" {PreviousPageLink} {CurrentPageReport} {NextPageLink}"
                                            paginatorPosition="top" emptyMessage="No user invited" >
                                    <h:outputText value="#{notificationBean.getUserNameFromNotification(notification)}" />

                                </p:dataList>

                            </p>
                            <c:choose>
                                <c:when test="#{userHome.selectedEvent.creator.email == editProfile.email}">

                                    <p:commandButton actionListener="#{ownEvent.delete(userHome.selectedEvent)}" value="Delete"  oncomplete="PF('eventSummary').hide();" update="calendarForm" />
                                    <p:commandButton actionListener="#{createEvent.setEventToCreate(userHome.selectedEvent)}" value="Edit" oncomplete="PF('eventSummary').hide();PF('eventDialog').show();" update="_creation_eventDetails"/>

                                </c:when>

                                <c:when test="#{userHome.selectedEvent.creator.email != editProfile.email}">

                                    <p:commandButton actionListener="#{acceptedEvent.delete(userHome.selectedEvent, userHome.currentUser)}" value="Delete"  oncomplete="PF('eventSummary').hide();" update="calendarForm" />

                                </c:when>
                            </c:choose>


                        </p:outputPanel>


                    </p:dialog>
                </h:form>
                <h:form id="creation">
                    <p:dialog widgetVar="eventDialog" header="Event Details" showEffect="clip" hideEffect="clip" onHide="#{userHome.init()}">

                        <h:panelGrid id="eventDetails" columns="2">
                            <h:outputLabel for="title" value="Title:" />
                            <p:inputText id="title" value="#{createEvent.eventToCreate.title}" required="true" />

                            <h:outputLabel for="from" value="From:" />
                            <p:calendar id="from" value="#{createEvent.eventToCreate.startingDate}" pattern="dd/MM/yyyy HH:mm" required="true"/>

                            <h:outputLabel for="to" value="To:" />
                            <p:calendar id="to" value="#{createEvent.eventToCreate.endingTime}" pattern="dd/MM/yyyy HH:mm" required="true"/>

                            <h:outputLabel for="location" value="Location:" />
                            <p:inputText id="location" value="#{createEvent.eventToCreate.location}" />

                            <h:outputLabel for="description" value="Description:" />
                            <p:inputTextarea rows="5" cols="20" counter="remaining-char" maxlength="260" counterTemplate="{0} characters remaining." autoResize="false"  value="#{createEvent.eventToCreate.description}"/>
                            <h:outputText value=" "/>
                            <h:outputText id="remaining-char" />

                            <p:outputLabel value="Users invited" />
                            <p:dataList value="#{createEvent.usersToInvite}" var="user" type="ordered" emptyMessage="No user invited">
                                #{user.name} #{user.surname}
                            </p:dataList>
                            <p:outputLabel value="Outdoor" />
                            <p:selectBooleanCheckbox value="#{createEvent.eventToCreate.outdoor}" />

                            <p:outputLabel value="Public" />
                            <p:selectBooleanCheckbox value="#{createEvent.eventToCreate.visibility}" />

                            <p:commandButton styleClass="onRightBtn" value="Invitation" ajax="true" update="creation" oncomplete="PF('eventDialog').show();PF('myinvitationDial').show();" />


                            <f:facet name="footer">
                                <p:commandButton type="reset" value="Reset" />
                                <p:commandButton styleClass="onRightBtn" actionListener="#{createEvent.checkEvent}" value="Save" ajax="true" update="_calendarForm" oncomplete="PF('eventDialog').hide();}" />
                            </f:facet>
                        </h:panelGrid>
                    </p:dialog>
                </h:form>
                <h:form id="invitation">

                    <p:dialog widgetVar="myinvitationDial" id="myinvitationDial" header="User Search Form" showEffect="clip" hideEffect="clip" >
                        <p:panel id="myinvitationDet">
                            <h:outputLabel for="search" value="Search a user:" />
                            <p:inputText id="search" value="#{createEvent.user_email}" required="" />

                            <p:commandButton actionListener="#{createEvent.submitUserSearch}" update="resultsearch" value="Search" ajax="true" oncomplete="PF('myinvitationDial').show();" />
                            <br/>
                            <h:outputLabel id="resultsearchLabel" value="Result search: " />
                            <h:outputText id="resultsearch" value="#{createEvent.userFound}"/>
                            <p:commandButton id="submit" actionListener="#{createEvent.manageUser}" value="Submit" update="invitedList" oncomplete="PF('myinvitationDial').show();" /><br/>
                            <p:outputLabel value="Users invited: " />
                            <p:dataList value="#{createEvent.usersToInvite}" var="user" type="ordered" emptyMessage="No user invited" id="invitedList">
                                #{user.name} #{user.surname}   
                                <p:commandButton actionListener="#{createEvent.removeUser(createEvent.user)}" id="iconOnly" update="invitedList" icon="ui-icon-closethick" title="Remove User" oncomplete="PF('myinvitationDial').show();" />
                            </p:dataList>

                            <f:facet name="footer">
                                <p:commandButton type="cancel" actionListener="#{createEvent.emptyUsersToInvite}" value="Back" update="_creation_eventDetails" oncomplete="PF('myinvitationDial').hide();" />
                                <p:commandButton styleClass="onRightBtn" value="Save Invitation" ajax="true" update="_creation_eventDetails" oncomplete="PF('myinvitationDial').hide();" />
                            </f:facet>
                        </p:panel>
                    </p:dialog>
                </h:form>
                <h:form id="eventsoverlapping">
                    <p:dialog widgetVar="myoverlappings" id="myoverlappings" header="Events overlapping with your future event" showEffect="clip" hideEffect="clip">
                        <p:panel id="overlapDet">
                            <c:if test="#{not empty createEvent.eventsOverlapping}" >
                                <p:dataList value="#{createEvent.eventsOverlapping}" var="event" type="unordered" emptyMessage="No overlapping">

                                    #{event.title}    From:    #{event.startingDate}    To:    #{event.endingTime}
                                </p:dataList>
                                <p:commandButton id="accept" actionListener="#{createEvent.acceptOverlap(createEvent.eventToCreate)}" value="Accept Overlap" oncomplete="PF('myoverlappings').hide();" update="@all"/>
                                <p:commandButton id="refuse" actionListener="#{createEvent.editFromOverlap}" value="Refuse Overlap" oncomplete="PF('myoverlappings').hide();" />
                            </c:if>
                        </p:panel>
                    </p:dialog>
                </h:form>

            </ui:define>

        </ui:composition>


    </body>

</html>
